package com.tcoiss.app.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tcoiss.app.domain.AscoOrderForm;
import com.tcoiss.app.service.IAscoOrderFormService;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 财神订单Controller
 * 
 * @author zw
 * @date 2021-08-30
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/ascoOrder" )
public class AscoOrderFormController extends BaseController {

    private final IAscoOrderFormService iAscoOrderFormService;

    /**
     * 查询财神订单列表
     */
    @PreAuthorize(hasPermi = "integration:ascoOrder:list")
    @GetMapping("/list")
    public TableDataInfo list(AscoOrderForm ascoOrderForm) {
        startPage();
        List<AscoOrderForm> list = iAscoOrderFormService.queryList(ascoOrderForm);
        return getDataTable(list);
    }

    /**
     * 导入神订单列表
     */
    @PreAuthorize(hasPermi = "bus:table:import")
    @Log(title = "业务表", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<AscoOrderForm> util = new ExcelUtil<AscoOrderForm>(AscoOrderForm.class);
        List<AscoOrderForm> columns = util.importExcel(file.getInputStream());
        String message = iAscoOrderFormService.importTable(columns, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导出财神订单列表
     */
    @PreAuthorize(hasPermi = "integration:ascoOrder:export" )
    @Log(title = "财神订单" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,AscoOrderForm ascoOrderForm) throws IOException
    {
        List<AscoOrderForm> list = iAscoOrderFormService.queryList(ascoOrderForm);
        ExcelUtil<AscoOrderForm> util = new ExcelUtil<AscoOrderForm>(AscoOrderForm.class);
        util.exportExcel(response,list, "ascoOrder" );
    }

    /**
     * 获取财神订单详细信息
     */
    @PreAuthorize(hasPermi = "integration:ascoOrder:query" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iAscoOrderFormService.getById(id));
    }

    /**
     * 新增财神订单
     */
    @PreAuthorize(hasPermi = "integration:ascoOrder:add" )
    @Log(title = "财神订单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AscoOrderForm ascoOrderForm) {
        return toAjax(iAscoOrderFormService.save(ascoOrderForm) ? 1 : 0);
    }

    /**
     * 修改财神订单
     */
    @PreAuthorize(hasPermi = "integration:ascoOrder:edit" )
    @Log(title = "财神订单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AscoOrderForm ascoOrderForm) {
        return toAjax(iAscoOrderFormService.updateById(ascoOrderForm) ? 1 : 0);
    }

    /**
     * 删除财神订单
     */
    @PreAuthorize(hasPermi = "integration:ascoOrder:remove" )
    @Log(title = "财神订单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iAscoOrderFormService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 删除消息同步记录
     */
    @PreAuthorize(hasPermi = "integration:ascoOrder:remove" )
    @Log(title = "财神订单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/clean" )
    public AjaxResult handleDeleteAll() {
        boolean returnFlag=iAscoOrderFormService.remove(new QueryWrapper<>());
        return toAjax( returnFlag? 1 : 0);
    }
}
